const TEXT = {
  fontSize: 48,
  x: null,
  y: null,
  align: 'center',
  verticalAlign: 'middle',
  text: '',
  fontStyle: 'bold',
  fontFamily: '"Clear Sans", "Helvetica Neue", Arial, sans-serif',
  fill: '#f9f6f2'
}

var width = document.documentElement.clientWidth
//初始化画布宽高，适配移动端不同机型
if (width >= 980) {
  TEXT.fontSize = 48
} else if (width >= 650) {
  TEXT.fontSize = 40
} else {
  TEXT.fontSize = 28
}

function CellText(cell) {
  this.init(cell)
}

CellText.prototype.init = function (cell) {
  this.config = JSON.parse(JSON.stringify(TEXT))
  this.config.width = cell.width
  this.config.height = cell.height

  this.Text = null
}
// 设置text实例
CellText.prototype.set = function(text){
  this.Text = text

}
CellText.prototype.setXY = function(x,y){
  this.config.x = x
  this.config.y = y
}
// 更改文字
CellText.prototype.setText = function (val) {
  this.config.text = val
  this.setColor()
}
// 设置颜色
CellText.prototype.setColor = function (color) {
  this.config.fill = this.getColor()
}
// 出现
CellText.prototype.appear = function (x,y,value) {
  this.setText(value)
  this.tween = new Konva.Tween({
    node: this.Text,
    duration: 0.2,
    x: x,
    y: y,
    width: this.config.width,
    height: this.config.height,
    fill: this.getColor(value),
    easing: Konva.Easings.EaseOut
  })
  this.tween.play()
}
// 移动
CellText.prototype.move = function (x,y) {
  this.tween = new Konva.Tween({
    node: this.Text,
    duration: 0.2,
    x: x,
    y: y,
    width: this.config.width,
    height: this.config.height,
    fill: this.getColor(),
    easing: Konva.Easings.EaseInOut
  })
  this.tween.play()
}
// 查找颜色值
CellText.prototype.getColor = function (value) {
  let val = value || this.config.text
  if (val == null) {
    return ''
  } else if (val == 2) {
    return '#776e65'
  } else if (val == 4) {
    return '#776e65'
  } else if (val == 8) {
    return '#f9f6f2'
  } else if (val == 16) {
    return '#f9f6f2'
  } else if (val == 32) {
    return '#f9f6f2'
  } else if (val == 64) {
    return '#f9f6f2'
  } else if (val == 128) {
    return '#f9f6f2'
  } else if (val == 256) {
    return '#f9f6f2'
  } else if (val == 512) {
    return '#f9f6f2'
  } else if (val == 1024) {
    return '#f9f6f2'
  } else if (val == 2048) {
    return '#f9f6f2'
  } else {
    return '#f9f6f2'
  }
}
export default CellText